import { createRouter, createWebHashHistory } from "vue-router";
const user = JSON.parse(sessionStorage.getItem("user"));
console.log(user);

const routes = [
    {
    path: "/",
    name: "login",
    component: () => import("../pages/login.vue"),
  },
  {
    path: "/home",
    name: "home",
    component: () => import("../pages/home.vue"),
  },
  {
    path: "/search",
    name: "search",
    component: () => import("../pages/search.vue"),
  },
  {
    path: "/clock",
    name: "clock",
    component: () => import("../pages/clock.vue"),
  },
 
];
const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
// 添加路由守卫
// router.beforeEach((to, from) => {
//   // 如果用户未登录且试图访问非登录页面，重定向到登录页面
//   if (!user && to.path !== "/login") {
//     return { name: "login" }; // 使用命名路由更加可靠
//   }

//   // 如果用户已登录且访问登录页面，重定向到首页
//   if (user && to.path === "/login") {
//     return { name: "home" }; // 使用命名路由更加可靠
//   }
// });

export default router;